package com.tjsinfo.tjsoa.fileExpress.bo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.Cookie;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;

import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.common.page.UserInfoPage;
import com.tjsinfo.tjsoa.fileExpress.vo.FileBean;
import com.tjsinfo.tjsoa.fileExpress.vo.FileExpressFile;
import com.tjsinfo.tjsoa.fileExpress.vo.FileExpressGroup;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;

public class FileExpressFilePageBo implements IFileExpressFilePageBo {
	private IQbcDao qbcDao;
	private IFileExpressGroupBo expressGroupBo;
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_User.IAddressListUserPageBo#getQbcDao()
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_File.IFileExpressFilePageBo#getQbcDao()
	 */
	public IQbcDao getQbcDao() {
		return qbcDao;
	}
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_User.IAddressListUserPageBo#setQbcDao(com.tjsinfo.tjsoa.Dao.IQbcDao)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_File.IFileExpressFilePageBo#setQbcDao(com.tjsinfo.tjsoa.Dao.IQbcDao)
	 */
	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}
	
	private static int pagecount = 0;
	public static int getPagecount() {
		return pagecount;
	}
	
	private static String group = "0";
	public static String getGroup() {
		return group;
	}
	
	private static int page = 0;
	public static int getPage() {
		return page;
	}
	public static int pagenum = 3;
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_User.IAddressListUserPageBo#getPrivateGotoPage(java.lang.String, int)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_File.IFileExpressFilePageBo#getGotoPage(java.lang.String, int, java.lang.String)
	 */
	public List<FileBean> getGotoPage(String group_id,int page,String pagenum1,String orderColumn,String order) {
		SimpleExpression a = Restrictions.eq("id", group_id);
		
		DetachedCriteria criteria = DetachedCriteria.forClass(FileExpressFile.class);
		criteria.add(Restrictions.isNull("isdel"));
		if (orderColumn!=null && !"".equals(orderColumn)) {
			if ("datetime".equals(orderColumn)) {
				if ("asc".equals(order)) {
					criteria.addOrder(Order.asc(orderColumn));
				}else {
					criteria.addOrder(Order.desc(orderColumn));
				}
			}else {
				//criteria.addOrder(Order.desc("datetime"));
				DetachedCriteria criteria1 = criteria.createCriteria("tjsUser");
				if ("asc".equals(order)) {
					criteria1.addOrder(Order.asc("userName"));
				}else {
					criteria1.addOrder(Order.desc("userName"));
				}
			}
		}
		DetachedCriteria criteria2 = criteria.createCriteria("fileExpressGroup");
		criteria2.add(a);
		ArrayList list = (ArrayList)qbcDao.getCriteria(criteria);
		list = (ArrayList) this.FileExpressToFileBean(list);
		UserInfoPage userPage = new UserInfoPage(list);
		if (pagenum1 != null) {
			userPage.setPageSize(Integer.valueOf(pagenum1));
		}else {
			userPage.setPageSize(pagenum);
		}
		pagecount = userPage.getPageCount();
		if (page > pagecount) {
			userPage.gotoPage(pagecount);
		}else {
			userPage.gotoPage(page);
		}
		//userPage.setPageIndex(2);
		ArrayList list1 = new ArrayList(userPage.getPageData());
		return list1;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_User.IAddressListUserPageBo#getPrivateLastPage(java.lang.String)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_File.IFileExpressFilePageBo#getLastPage(java.lang.String, java.lang.String)
	 */
	public List<FileBean> getLastPage(String group_id,String pagenum1,String orderColumn,String order) {
		SimpleExpression a = Restrictions.eq("id", group_id);
		
		DetachedCriteria criteria = DetachedCriteria.forClass(FileExpressFile.class);
		criteria.add(Restrictions.isNull("isdel"));
		SimpleExpression b = Restrictions.eq("state", 0);
		criteria.add(b);
		DetachedCriteria criteria2 = criteria.createCriteria("fileExpressGroup");
		criteria2.add(a);
		if (orderColumn!=null && !"".equals(orderColumn)) {
			if ("datetime".equals(orderColumn)) {
				if ("asc".equals(order)) {
					criteria.addOrder(Order.asc(orderColumn));
				}else {
					criteria.addOrder(Order.desc(orderColumn));
				}
			}else {
				criteria.addOrder(Order.desc("datetime"));
				DetachedCriteria criteria1 = criteria.createCriteria("tjsUser");
				if ("asc".equals(order)) {
					criteria1.addOrder(Order.asc("userName"));
				}else {
					criteria1.addOrder(Order.desc("userName"));
				}
			}
		}
		ArrayList list = (ArrayList)qbcDao.getCriteria(criteria);
		list = (ArrayList) this.FileExpressToFileBean(list);
		UserInfoPage userPage = new UserInfoPage(list);
		
		if (pagenum1 != null) {
			userPage.setPageSize(Integer.valueOf(pagenum1));
		}else {
			userPage.setPageSize(pagenum);
		}
		page = userPage.getPageCount();
		userPage.gotoPage(userPage.getPageCount());
		pagecount = userPage.getPageCount();
		//userPage.setPageIndex(2);
		ArrayList list1 = new ArrayList(userPage.getPageData());
		return list1;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_User.IAddressListUserPageBo#getPrivateFirstPage(int)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_File.IFileExpressFilePageBo#getFirstPage(int, java.lang.String, java.lang.String)
	 */
	public List<FileBean> getFirstPage(int user_id,String pagenum1) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FileExpressGroup.class);
		TSystemUser user = new TSystemUser();
		user.setUserId(Integer.valueOf(user_id));
		criteria.add(Restrictions.and(Restrictions.eq("groupName", "我的接收文件夹"), Restrictions.eq("tjsUser", user)));
		
		ArrayList<FileExpressGroup> grouplist = new ArrayList<FileExpressGroup>();
		try {
			grouplist =(ArrayList<FileExpressGroup>) qbcDao.getCriteria(criteria);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if (grouplist.size()==0) {
			group = UUID.randomUUID().toString().replace("-", "");
			expressGroupBo.insertGroup(group,"我的接收文件夹", user_id);
			
			FileExpressGroup group1 = new FileExpressGroup();
			group1.setId(group);
			grouplist.add(group1);
		}else {
			group = grouplist.iterator().next().getId();
		}
		
		SimpleExpression f = Restrictions.eq("fileExpressGroup", grouplist.iterator().next());
		
		DetachedCriteria criteria2 = DetachedCriteria.forClass(FileExpressFile.class);
		criteria2.add(Restrictions.isNull("isdel"));
		criteria2.add(f);
		//SimpleExpression b = Restrictions.eq("state", 0);
		//criteria2.add(b);
		criteria2.addOrder(Order.desc("datetime"));
		ArrayList list = (ArrayList)qbcDao.getCriteria(criteria2);
		list = (ArrayList) this.FileExpressToFileBean(list);
		UserInfoPage userPage = new UserInfoPage(list);
		
		if (pagenum1 != null) {
			userPage.setPageSize(Integer.valueOf(pagenum1));
		}else {
			userPage.setPageSize(pagenum);
		}
		userPage.gotoPage(1);
		pagecount = userPage.getPageCount();
		//userPage.setPageIndex(2);
		ArrayList list1 = new ArrayList(userPage.getPageData());
		return list1;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_User.IAddressListUserPageBo#getPageCount(java.lang.String)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_File.IFileExpressFilePageBo#getPageCount(java.lang.String, java.lang.String)
	 */
	public int getPageCount(String group_id,String pagenum1) {
		SimpleExpression a = Restrictions.eq("id", group_id);
		
		DetachedCriteria criteria = DetachedCriteria.forClass(FileExpressFile.class);
		criteria.add(Restrictions.isNull("isdel"));
		SimpleExpression b = Restrictions.eq("state", 0);
		criteria.add(b);
		DetachedCriteria criteria2 = criteria.createCriteria("fileExpressGroup");
		criteria2.add(a);
		ArrayList list = (ArrayList)qbcDao.getCriteria(criteria);
		list = (ArrayList) this.FileExpressToFileBean(list);
		UserInfoPage userPage = new UserInfoPage(list);
		
		if (pagenum1 != null) {
			userPage.setPageSize(Integer.valueOf(pagenum1));
		}else {
			userPage.setPageSize(pagenum);
		}
		return userPage.getPageCount();
	}
	
	private List<FileBean> FileExpressToFileBean(List list){
		List list2 = new ArrayList();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			FileExpressFile file = (FileExpressFile) iterator.next();
			FileBean bean = new FileBean();
			bean.setId(file.getId());
			bean.setJieshouUser(file.getFileExpressGroup().getTjsUser().getUserName());
			bean.setFileName(file.getFileName());
			bean.setExpressUser(file.getTjsUser().getUserName());
			bean.setDatetime(sdf.format(file.getDatetime()));
			bean.setExpressLevel(file.getExpressLevel().toString());
			bean.setFileSize(file.getFileSize());
			bean.setFilePath(file.getFilePath());
			bean.setRemark(file.getRemark());
			list2.add(bean);
		}
		return list2;
	}
	public void setExpressGroupBo(IFileExpressGroupBo expressGroupBo) {
		this.expressGroupBo = expressGroupBo;
	}
}
